package com.lfy.modules.scheduler.controller;

import com.lfy.domain.entity.JobDTO;
import com.lfy.domain.entity.JobNoCreateVO;
import com.lfy.domain.entity.JobTaskListVO;
import com.lfy.modules.common.anno.Authentication;
import com.lfy.modules.common.anno.NoNeedLogin;
import com.lfy.modules.safety.aop.anno.CurrentLimit;
import com.lfy.modules.common.domain.R;
import com.lfy.modules.safety.limit.strategy.LimitStrategyEnum;
import com.lfy.modules.scheduler.service.TaskService;
import com.lfy.utils.TaskUtil;
import org.quartz.Scheduler;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
 *  动态定时任务接口
 *
 * @author <a href="https://www.lvfeiy.top">lvfeiyang</a>
 * @date 2024/4/25 1:00
 */
@RestController
@RequestMapping("/task")
public class TaskController {
    @Resource
    private Scheduler scheduler;
    @Resource
    private TaskService taskService;

    /**
     * 创建定时任务
     */
    @PostMapping("/create")
    @Authentication("sys:task:update")
    @NoNeedLogin
    public R<Void> createTask(@Validated @RequestBody JobDTO jobEntity){
        TaskUtil.createTask(jobEntity,scheduler);
        return R.success();
    }

    /**
     * 暂停任务
     * @param jobName 任务名称
     * @return response
     */
    @PostMapping("/pause")
    @NoNeedLogin
    @Authentication("sys:task:update")
    public R<Void> pauseTask(String jobName){
        TaskUtil.pauseTask(scheduler,jobName);
        return R.success();
    }

    /**
     * 恢复任务
     * @param jobName 任务名称
     * @return R
     */
    @PostMapping("/resume")
    @NoNeedLogin
    @Authentication("sys:task:update")
    public R<Void> resumeTask(String jobName){
        TaskUtil.resumeTask(scheduler,jobName);
        return R.success();
    }

    /**
     * 删除任务
     * @param jobName 任务名称
     * @return R
     */
    @PostMapping("/delete")
    @NoNeedLogin
    @Authentication("sys:task:update")
    public R<Void> deleteTask(String jobName){
        TaskUtil.deleteTask(scheduler,jobName);
        return R.success();
    }

    /**
     * 修改任务
     * @param jobEntity 任务实体
     * @return R
     */
    @PostMapping("/modify")
    @NoNeedLogin
    @Authentication("sys:task:update")
    public R<Void> modifyTask(@Validated @RequestBody JobDTO jobEntity){
        TaskUtil.modifyTask(scheduler,jobEntity);
        return R.success();
    }

    /**
     * 获取任务列表
     * @return R
     */
    @GetMapping("/get/taskList")
    @NoNeedLogin
    @Authentication("sys:task:get")
    public R<List<JobTaskListVO>> getTaskStatus(){

        return R.success(taskService.getTaskList());
    }

    /**
     * 获取未创建的任务列表
     * @return
     */
        @GetMapping("/get/noCreateTask")
    @NoNeedLogin
    @Authentication("sys:task:get")
    public R<List<JobNoCreateVO>> getNoCreateTask(){
        return R.success(taskService.getNoCreateTask());
    }
    /**
     * 执行任务一次
     */

    @PostMapping("/runOnce")
    @NoNeedLogin
    @Authentication("sys:task:update")
    public R<Void> runOnce(String jobName){
        TaskUtil.runTaskOnce(scheduler,jobName);
        return R.success();
    }
    @NoNeedLogin
    @CurrentLimit(time = 5,once=5,nuit = TimeUnit.SECONDS,strategy = LimitStrategyEnum.Ip)
    @CurrentLimit(time = 1,once=20,nuit = TimeUnit.MINUTES,strategy = LimitStrategyEnum.Ip)
    @GetMapping("/test")
    public R<Void> test(){
        return R.success();
    }
}
